package com.example.shopdemo.repository;

import com.example.shopdemo.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-21
 **/
public interface ProductRepository extends JpaRepository<Product,Long>,JpaSpecificationExecutor<Product> {

    List<Product> findByProductCategoryId(Long productCategoryId);

    @Transactional
    @Modifying
    @Query(value = "update product p set p.product_category_id = null where product_category_id =:productCategoryId",nativeQuery = true)
    void updateProductCategoryToNull(@Param("productCategoryId")Long productCategoryId);

}

